/*
 * -------------------------------------------------------------------------------
 * @FilePath     : app_gsensor.h
 * @Description  : application
 * Copyright (c) 2023 by NanoIC, All Rights Reserved. 
 * @Date         : 2023-11-14 17:59:59
 * @LastEditTime : 2023-11-14 18:01:16
 * @Author       : LvH
 * -------------------------------------------------------------------------------
 */

#ifndef __APP_G_SENSOR_H__
#define __APP_G_SENSOR_H__
/* Includes ------------------------------------------------------------------*/
// #include "app_cfg.h"
#include "app_iic.h"
#include "common.h" 
#include "sys_console.h"
#include "pin.h"
#include "one_wire_bus_app.h"
#include "app_key.h" 

#define G_REG_LEN_MAX       0x02
#define G_SENSOR_ADDR       0x4C<<1

#define G_DEVICE_STATUS     0x05


//原始值
extern short original_x;
extern short original_y;
extern short original_z;

//水平标准值
extern short coefficient_x;
extern short coefficient_y;
extern short coefficient_z;

//校准系数
extern short horizont_x;
extern short horizont_y;
extern short horizont_z;

//hall timer
#define HALL_TIMER_N        5000   //30 //3ms
#define GSENSOR_TIMER_N        40   //30 //3ms
//HALL

//G_sensor
#define G_sensor_PIN        PA015
#define G_sensor_INPUT      PA015_INPUT



//INT io
#define PINMASK_GSENSOR_IIC_INT                     15

#define IPP_PUSHPULL	0x40
#define IPP_OPENDRAIN   0x00
#define IAH_ACTIVEHIGH	0x80
#define IAH_ACTIVELOW  0x00

typedef enum
{
	MC3416_128HZ=0,
	MC3416_256HZ=1,
	MC3416_512HZ=2,
	MC3416_1024HZ=5,
}Mc3416_ODR;

typedef enum
{
	MC3416_2G=0x00,
	MC3416_4G=0x10,
	MC3416_8G=0x20,
	MC3416_16G=0x30,
	MC3416_12G=0x40,
}Mc3416_RANGE;

typedef enum
{
	WAKEMODE=1,
	STANDBYMODE=3,	
}Mc3416_MODE;

extern uint8_t global_gsensor_start;
extern volatile bool global_gsensor_enable;
extern volatile bool global_gsensor_send;
extern volatile bool gsensor_info_send_flag;

extern uint8_t gsensor_raw_value_flag;
extern uint8_t gsensor_level_cal_flag;
extern uint8_t gsensor_cal_value_flag;
extern uint8_t gsensor_cal_cfg_flag;
extern uint8_t gsensor_data_up_flag;
extern uint8_t gsensor_clear_cal_data_flag;
extern uint8_t gsensor_cal_offset_flag;

extern uint8_t gsensor_cal[6];
extern uint16_t cal_offset;
 
void app_hall_timer_0_1ms_callback(void);
void app_hall_or_gsensor_pin_init(void);
void app_hall_process(void);

/**
 * @brief gsensor 进程
 * 
 */
void app_gsensor_process(void);

void app_gsensor_to_pad(void);
/**
 * @brief Gsensor初始化
 */
void app_Gsensor_init(void);

void app_Gsensor_sleep_io_set(void);

void app_gsensor_timer_tick_0_1ms_callback(void);
/**
 * @brief 设置进入低功耗
 * 
 * @param on true 唤醒  false休眠
 */
void app_gsensor_sleep_io_set(bool on);


void regular_read_gsensor_40ms_data(void);
#endif
